<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="新增客户" prop="eCustomernum">
      <el-input v-model="dataForm.eCustomernum" placeholder="新增客户"></el-input>
    </el-form-item>
    <el-form-item label="新增商机" prop="eBusinessopnum">
      <el-input v-model="dataForm.eBusinessopnum" placeholder="新增商机"></el-input>
    </el-form-item>
    <el-form-item label="新增联系人" prop="eLiaisionnum">
      <el-input v-model="dataForm.eLiaisionnum" placeholder="新增联系人"></el-input>
    </el-form-item>
    <el-form-item label="新增预约" prop="eAppointmentum">
      <el-input v-model="dataForm.eAppointmentum" placeholder="新增预约"></el-input>
    </el-form-item>
    <el-form-item label="新增合同" prop="eContractnum">
      <el-input v-model="dataForm.eContractnum" placeholder="新增合同"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          eMonth: 0,
          eCustomernum: '',
          eBusinessopnum: '',
          eLiaisionnum: '',
          eAppointmentum: '',
          eContractnum: ''
        },
        dataRule: {
          eCustomernum: [
            { required: true, message: '新增客户不能为空', trigger: 'blur' }
          ],
          eBusinessopnum: [
            { required: true, message: '新增商机不能为空', trigger: 'blur' }
          ],
          eLiaisionnum: [
            { required: true, message: '新增联系人不能为空', trigger: 'blur' }
          ],
          eAppointmentum: [
            { required: true, message: '新增预约不能为空', trigger: 'blur' }
          ],
          eContractnum: [
            { required: true, message: '新增合同不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.eMonth = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.eMonth) {
            this.$http({
              url: this.$http.adornUrl(`/com.emp/saleexecutedata/info/${this.dataForm.eMonth}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.eCustomernum = data.saleexecutedata.eCustomernum
                this.dataForm.eBusinessopnum = data.saleexecutedata.eBusinessopnum
                this.dataForm.eLiaisionnum = data.saleexecutedata.eLiaisionnum
                this.dataForm.eAppointmentum = data.saleexecutedata.eAppointmentum
                this.dataForm.eContractnum = data.saleexecutedata.eContractnum
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/com.emp/saleexecutedata/${!this.dataForm.eMonth ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'eMonth': this.dataForm.eMonth || undefined,
                'eCustomernum': this.dataForm.eCustomernum,
                'eBusinessopnum': this.dataForm.eBusinessopnum,
                'eLiaisionnum': this.dataForm.eLiaisionnum,
                'eAppointmentum': this.dataForm.eAppointmentum,
                'eContractnum': this.dataForm.eContractnum
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
